package com.zx.wangyixinwen.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.zx.wangyixinwen.R;
import com.zx.wangyixinwen.activity.VodioActivity;
import com.zx.wangyixinwen.adapter.SeeAdatper;
import com.zx.wangyixinwen.bean.Vodio;
import com.zx.wangyixinwen.bean.VodioDeatls;
import com.zx.wangyixinwen.refresh.SwipeRefreshLoadingLayout;

import java.util.ArrayList;
import java.util.List;

import static com.zx.wangyixinwen.R.id.see_refresh;


/**
 * Created by dllo on 16/1/4.
 */
public class ReadFragment extends Fragment implements SwipeRefreshLoadingLayout.OnRefreshListener{
    private ListView listView;
    private SeeAdatper adatper;
    private List<Vodio> datas;
    private Context context;
    private SwipeRefreshLoadingLayout swipeRefreshLoadingLayout;
    private RequestQueue queue;
    private String url = "http://c.3g.163.com/nc/video/home/0-10.html";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_read, null);
        listView = (ListView) view.findViewById(R.id.listView_read);
        swipeRefreshLoadingLayout = (SwipeRefreshLoadingLayout) view.findViewById(see_refresh);
        swipeRefreshLoadingLayout.setOnRefreshListener(this);
        addData();
        return view;
    }

    private void addData() {
        queue = Volley.newRequestQueue(getActivity());
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                datas = new ArrayList<>();
                Vodio vodio = gson.fromJson(response,Vodio.class);
                datas.add(vodio);
                for (int i = 0; i < vodio.getVideoList().size(); i++) {
                    VodioDeatls vodioDeatls = new VodioDeatls();
                    vodioDeatls.setCover(datas.get(0).getVideoList().get(i).getCover());
                    vodioDeatls.setTitle(datas.get(0).getVideoList().get(i).getTitle());
                    vodioDeatls.setDescription(datas.get(0).getVideoList().get(i).getDescription());
                }
                adatper = new SeeAdatper(datas, context);
                listView.setAdapter(adatper);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        VodioActivity.goToVodio(context,datas.get(0).getVideoList().get(position));
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(stringRequest);
    }

    @Override
    public void onRefresh() {
        addData();
        swipeRefreshLoadingLayout.setRefreshing(false);
    }
}